package com.c.ctgproject.adapter.doorkeeper

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import com.c.ctgproject.entity.doorkeeper.DoorkeeperPartOutEntity
import com.c.ctgproject.utils.handlerNull

/**
 *作者:LiBW
 *创建日期: 2020/9/12
 *描述:配件出库adapter
 */
class DoorkeeperPartListAdapter(private val context: Context, private val mList: List<DoorkeeperPartOutEntity.DataBean.ListBean>, private val callback: DoorkeeperPartCallback) : RecyclerView.Adapter<DoorkeeperPartListAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_doorkeeper_part_list, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data = mList[position]

        data.partsInfos?.let {
            val mAdapter = DoorkeeperPartListChildAdapter(context, it)
            holder.mCardChildRV.adapter = mAdapter
            holder.mCardChildRV.layoutManager = LinearLayoutManager(context)
        }
        holder.mSellerNameTV.text = data.salesPersonName.handlerNull()
        holder.mOrderID.text = data.partsSalesNo.handlerNull()

        holder.mConfirmBTN.setOnClickListener {
            callback.carConfirm(data.partsSalesOrderId)
        }
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val mOrderID: TextView = itemView.findViewById(R.id.mOrderID)
        val mSellerNameTV: TextView = itemView.findViewById(R.id.mSellerNameTV)
        val mConfirmRL: RelativeLayout = itemView.findViewById(R.id.mConfirmRL)
        val mCardChildRV: RecyclerView = itemView.findViewById(R.id.mCardChildRV)
        val mConfirmBTN: Button = itemView.findViewById(R.id.mConfirmBTN)
    }

    interface DoorkeeperPartCallback {
        fun carConfirm(partsSalesOrderId: Int)
    }

}